<?php
// 开启会话
session_start();

// 初始化游戏数据
if (!isset($_SESSION['resources'])) {
    $_SESSION['resources'] = [
        'wood' => 100,
        'stone' => 50
    ];
}

if (!isset($_SESSION['buildings'])) {
    $_SESSION['buildings'] = [];
}

// 定义建筑及其资源消耗
$buildings = [
    'house' => [
        'name' => '房屋',
        'wood' => 20,
        'stone' => 10
    ],
    'tower' => [
        'name' => '塔楼',
        'wood' => 30,
        'stone' => 20
    ]
];

// 处理建造操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['build'])) {
    $buildingType = $_POST['building'];
    $building = $buildings[$buildingType];
    if ($_SESSION['resources']['wood'] >= $building['wood'] && $_SESSION['resources']['stone'] >= $building['stone']) {
        // 消耗资源
        $_SESSION['resources']['wood'] -= $building['wood'];
        $_SESSION['resources']['stone'] -= $building['stone'];
        // 记录已建造的建筑
        $_SESSION['buildings'][] = $building['name'];
        $message = "成功建造 {$building['name']}！";
    } else {
        $message = "资源不足，无法建造 {$building['name']}。";
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>建造类网页游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        p {
            margin-bottom: 10px;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        select {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }

        .building-list {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>

<body>
    <h1>建造类网页游戏</h1>
    <p>当前资源：</p>
    <ul>
        <li>木材：<?php echo $_SESSION['resources']['wood']; ?></li>
        <li>石头：<?php echo $_SESSION['resources']['stone']; ?></li>
    </ul>

    <?php if (isset($message)): ?>
        <div class="message <?php echo strpos($message, '成功')!== false? 'success' : 'error'; ?>">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>

    <form method="post">
        <label for="building">选择要建造的建筑：</label>
        <select id="building" name="building">
            <?php foreach ($buildings as $type => $building): ?>
                <option value="<?php echo $type; ?>">
                    <?php echo $building['name']; ?>（木材：<?php echo $building['wood']; ?>，石头：<?php echo $building['stone']; ?>）
                </option>
            <?php endforeach; ?>
        </select>
        <button type="submit" name="build">建造</button>
    </form>

    <div class="building-list">
        <h2>已建造的建筑</h2>
        <?php if (empty($_SESSION['buildings'])): ?>
            <p>目前还没有建造任何建筑。</p>
        <?php else: ?>
            <ul>
                <?php foreach ($_SESSION['buildings'] as $building): ?>
                    <li><?php echo $building; ?></li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </div>
</body>

</html>